package cn.htsyb.synergy.security.web.captcha.controller;

import cn.htsyb.synergy.security.web.captcha.CaptchaBuilder;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

/**
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/22
 * @since 1.0
 */
@Controller
@Tag(name = "SecurityCaptchaController", description = "安全验证码接口")
public class SecurityCaptchaController {

    /**
     * 刷新验证码
     * @param request
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/security/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 设置响应的类型格式为图片格式
        response.setContentType("image/png");
        //禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        CaptchaBuilder captchaBuilder = new CaptchaBuilder();
        captchaBuilder.makerCaptcha();
        captchaBuilder.writeImageToStream(response.getOutputStream());

        HttpSession session = request.getSession();
        session.setAttribute("captcha", captchaBuilder.getCode());

    }
}
